package at.fhj.routenplaner;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class RoutingActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    
	Button btnGo;
	EditText edVon, edNach, edStadtHerkunft, edStadtZiel;
	TextView tvRoute;
	String url = "";
	private String von, nach, vonStadt, nachStadt, koord;
	private boolean koords;
	private int locomotion =0 , format =0;
	String test = "hallo";
	CheckBox checkBox1;

	 private static final int MNU_EDIT=0;
//   private static final String MNU_ShowAbout = "";
   
   public boolean onCreateOptionsMenu(Menu menu){
   	menu.add(Menu.NONE, MNU_EDIT, Menu.NONE, "Settings aufrufen");
   	return super.onCreateOptionsMenu(menu);
   }
   
   public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()){
		case MNU_EDIT:
			startActivity(new Intent(RoutingActivity.this, SettingsActivity.class));
			break;
		default:
			Log.i("MAIN", "Menu Item");
		}
   	return super.onOptionsItemSelected(item);
   	
   }
    
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.route);
        
        edVon = (EditText) findViewById(R.id.edVon);
        edNach = (EditText) findViewById(R.id.edNach);
        edStadtHerkunft = (EditText) findViewById(R.id.edStadtHerkunft);
        edStadtZiel = (EditText) findViewById(R.id.edStadtZiel);
    	checkBox1 = (CheckBox) findViewById(R.id.checkBox1);

        
        //Startort aus Settings laden
        SharedPreferences myPrefs = getSharedPreferences("mySharedSettings", 0 );
        edVon.setText(myPrefs.getString("street", ""));
        edStadtHerkunft.setText(myPrefs.getString("city", ""));
        if(myPrefs.getInt("box", 0)!= 0 ){
        	locomotion = myPrefs.getInt("box", 0 );
        	if(locomotion==2131034129)
        		locomotion = 1;
        	if(locomotion==2131034130)
        		locomotion = 2	;
        }
        
        if(myPrefs.getInt("box2", 0)!= 0 ){
        	format = myPrefs.getInt("box2", 0 );
        	if(format==2131034132)
        		format = 1;
        	if(format==2131034133)
        		format = 2	;
        }
        koords = myPrefs.getBoolean("checked", false);
        if(koords==true)
        	koord = "&generateCoordinates";
        else
        	koord = "";
        
        
        
        tvRoute = (TextView) findViewById(R.id.tvRoute);
        

        btnGo = (Button) findViewById(R.id.btnGo);
        btnGo.setOnClickListener(this);
        
    }

	@Override
	public void onClick(View v) {
		
		
		  von = edVon.getText().toString();
	      nach = edNach.getText().toString();
	      nachStadt = edStadtZiel.getText().toString();
	      vonStadt = edStadtHerkunft.getText().toString();
	      
	      
 
	         url = "http://ways4allcomp.fh-joanneum.at/GetRoute?originStreet="+von+"&originCity="+vonStadt+"&targetStreet="+nach+"&targetCity="+nachStadt+"&locomotionType="+locomotion+"&responseFormat="+format+koord;

	         
		StringBuffer sb = new StringBuffer();
		//
		try {

		BufferedReader inreader = null;
		URL url = new URL(this.url);
		HttpURLConnection conn = (HttpURLConnection) url
		.openConnection();
		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
		inreader = new BufferedReader(new InputStreamReader(
		conn.getInputStream()));
		String line;
		while ((line = inreader.readLine()) != null) {
		sb.append(line);
		}
		inreader.close();
		}
		conn = null;

		} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		sb.append(e.getMessage());
		}
		tvRoute.setText(sb.toString());
//	    tvRoute.setText(url);
	}
}